package com.fenghui.doubantop.utils;

import com.fenghui.doubantop.domain.Movie;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

/**
 * 电影解析工具类，继承自抽象的解析工具类
 */
public class MovieParseUtil extends ParseUtil<Movie> {

    @Override
    protected void extractInfo(Element child, Movie movie) {
        // 电影排名
        String movieRank = child.select("em").text();
        movie.setMovieRank(Integer.valueOf(movieRank));

        // 电影名称
        String movieTitle = child.select("div.info div.hd a span").text();
        // 片名
        String movieName = movieTitle.substring(0, movieTitle.indexOf("/")).trim();
        movie.setMovieName(movieName);

        // 别名
        String otherName = movieTitle.substring(movieTitle.indexOf("/") + 1).trim();
       movie.setOtherName(otherName);

        // 引述
        String quote = child.select("div.info div.bd p.quote span").text().trim();
        movie.setQuote(quote);

        String movieInfo = child.select("div.info div.bd p").get(0).outerHtml();
        // 主创人员
        String creativeStaff = extractCreativeStaff(movieInfo);
        movie.setCreativeStaff(creativeStaff);

        //获取电影块信息，并从中提起上映时间,产地、类型等
        String tmp = movieInfo.substring(movieInfo.indexOf("<br>") + 4, movieInfo.indexOf("</p>")).trim();
        // 年代
        String year = extractYear(tmp);
        movie.setYear(year);

        // 地区
        String area = extractArea(tmp);
        movie.setArea(area);

        // 主题
        String theme = extractTheme(tmp);
        movie.setTheme(theme);

        // 评分
        String rating_num = child.select("div.info div.bd div span.rating_num").text();
        movie.setRatingNum(Double.valueOf(rating_num));

        // 海报地址
        String movieUrl = child.select("div.pic a img").attr("src");
        movie.setMovieUrl(movieUrl);

        // 详情页地址 从中提取电影ID
        String movieDetail = child.select("div.pic a").attr("href");
        String movieId = BaseUtils.extractNumber(movieDetail).trim();
        movie.setMovieId(Integer.valueOf(movieId));
    }

    private String extractCreativeStaff(String movieInfo) {
        int directorStart = movieInfo.indexOf("导演");
        int brStart = movieInfo.indexOf("<br>");
        if (directorStart != -1 && brStart != -1 && directorStart < brStart) {
            String creativeStaff = movieInfo.substring(directorStart, brStart);
            creativeStaff = creativeStaff.replaceAll("&nbsp;", "").trim();
            return creativeStaff;
        }
        return "";
    }

    private String extractYear(String tmp) {
        String[] parts = tmp.split("/");
        if (parts.length > 0) {
            String year = parts[0].replaceAll("&nbsp;", "").trim();
            return year;
        }
        return "";
    }

    private String extractArea(String tmp) {
        String[] parts = tmp.split("/");
        if (parts.length > 1) {
            String area = parts[1].replaceAll("&nbsp;", "").trim();
            return area;
        }
        return "";
    }

    private String extractTheme(String tmp) {
        String[] parts = tmp.split("/");
        if (parts.length > 2) {
            String theme = parts[2].replaceAll("&nbsp;", "").trim();
            return theme;
        }
        return "";
    }

    @Override
    protected Element getElement(Document document) {
        return document.getElementsByClass("grid_view").get(0);
    }

    @Override
    protected boolean isIgnoredTag(Element element) {
        return false; // 根据电影解析的实际情况确定是否有需要忽略的标签
    }

    @Override
    protected Movie createItem() {
        return new Movie();
    }
}
